﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VgoCardChange.Models.Objects;
using VgoCardChange.Models.DAL;

namespace VgoCardChange.Models
{
    public class CateModels
    {
        public List<Cate> GetCates()
        {
            List<Cate> listcate = new List<Cate>();
            ConnectDB con = new ConnectDB();
            try
            {
                string sql = "select CateID,CateName,Description from TblCate";
                con.ExeReader(sql);
                while (con.dataRead.Read())
                {
                    Cate cate = new Cate();
                    cate.CateID =int.Parse(con.dataRead["CateID"].ToString());
                    cate.CateName = con.dataRead["CateName"].ToString();
                    cate.Description = con.dataRead["Description"].ToString();
                    listcate.Add(cate);
                }
            }
            catch (Exception ex)
            { Console.Write(ex); }
            finally { con.CloseData(); con = null; }
            return listcate;
        }
        public void CreateCate(Cate cate)
        {
            ConnectDB con = new ConnectDB();
            try
            {
                string sql = "sp_CreateCate  @CateName=N'"+cate.CateName+"'"+", @Description=N'"+cate.Description+"'";
                con.ExeNonQuery(sql);
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }
            finally { con.CloseData(); con = null; }
        }
        public void EditCate(Cate cate)
        {
            ConnectDB con = new ConnectDB();
            try
            {
                string sql = "EditCate @CateID='"+cate.CateID+"', @CateName=N'" + cate.CateName + "'" + ", @Description=N'" + cate.Description + "'";
                con.ExeNonQuery(sql);
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }
            finally { con.CloseData(); con = null; }
 
        }
        public void DeleteCate(int CateID)
        {
            ConnectDB con = new ConnectDB();
            try
            {
                string sql = "sp_DeleteCate @CateID='"+CateID+"'";
                con.ExeNonQuery(sql);
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }
            finally { con.CloseData(); con = null; }
        }
        public List<Cate> GetCateID(int CateID)
        {
            List<Cate> listcate = new List<Cate>();
            ConnectDB con = new ConnectDB();
            try
            {
                string sql = "sp_GetCateID @CateID='"+CateID+"'";
                con.ExeReader(sql);
                while (con.dataRead.Read())
                {
                    Cate cate = new Cate();
                    cate.CateID = int.Parse(con.dataRead["CateID"].ToString());
                    cate.CateName = con.dataRead["CateName"].ToString();
                    cate.Description = con.dataRead["Description"].ToString();
                    listcate.Add(cate);
                }
            }
            catch (Exception ex)
            { Console.Write(ex); }
            finally { con.CloseData(); con = null; }
            return listcate;
 
        }
        public List<Cate> GetCateName()
        {
            List<Cate> listcatename = new List<Cate>();
            ConnectDB con = new ConnectDB();
            try
            {
                string sql = "select CateID,CateName from TblCate";
                con.ExeReader(sql);
                while (con.dataRead.Read())
                {
                    Cate cate = new Cate();
                    cate.CateID = int.Parse(con.dataRead["CateID"].ToString());
                    cate.CateName = con.dataRead["CateName"].ToString();
                    listcatename.Add(cate);
                }
            }
            catch (Exception ex)
            { Console.Write(ex); }
            finally { con.CloseData(); con = null; }
            return listcatename;
        }
    }
}